package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Free;
import com.hl.sfzy.entity.Heartk;
import com.hl.sfzy.service.FreeService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/free")
@CrossOrigin
public class FreeController {

    @Autowired
    private FreeService freeService;

    @PostMapping("/insert")
    public R insert(@RequestBody Free free){
        return R.ok(freeService.insert(free));
    }
    @PutMapping("/update")
    public R update(@RequestBody Free free){
        return R.ok(freeService.update(free));
    }
    @DeleteMapping("/delete")
    public R delete(@RequestParam Integer id){
        int result=freeService.deleteById(id);
        if (result != 0) {
            return R.ok(result);
        } else {
            return R.fail("删除失败");
        }
    }
    @GetMapping("/select")
    public R select(@RequestParam Integer id){
        return R.ok(freeService.selectById(id));
    }
    @GetMapping("/selectAll")
    public R selectAll(){
        List<Free> list = freeService.selectAll();
        return R.ok(list);
    }
    @GetMapping("/findByPage")
    public R findByPage(@RequestParam(required = false,defaultValue = "1") int pageNum,
                        @RequestParam(required = false,defaultValue = "3") int pageSize,
                        Free free){
        PageInfo<Free> pageInfo=freeService.findByPage(pageNum, pageSize, free);
        return R.ok(pageInfo);
    }
}
